package com.aistar.controller;

import com.aistar.pojo.WorkExperience;
import com.aistar.service.WorkExperienceService;
import com.aistar.util.DateUtil;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.Date;

@Controller
public class WorkExperienceController {
    @Autowired
    private WorkExperienceService workExperienceService;

    @PostMapping("/workexperience")
    @ResponseBody
    public ServerResponse add(Integer resumeId, String startWorktime, String endWorkTime, String companyName,
                              String jobName, String companySize, String depatement, String companyType, String jobDescription,
                              String jobType ) {

        //数据类型转换
        Date startWorkTime =  DateUtil.string2DateYM(startWorktime);
        Date endWorktime =  DateUtil.string2DateYM(endWorkTime);

        //封装成WorkExperience 对象
        WorkExperience experience =   new WorkExperience();
        experience.setResumeId(resumeId);
        experience.setWorkStartTime(startWorkTime);
        experience.setWorkEndTime(endWorktime);
        experience.setWorkCompanyName(companyName);
        experience.setWorkPosition(jobName);
        experience.setWorkCompanyScaler(companySize);
        experience.setWorkDepartment(depatement);
        experience.setWorkCompanyProperty(companyType);
        experience.setWorkDesc(jobDescription);
        experience.setWorkType(jobType);
        experience.setStatus(0);


        ServerResponse serverResponse = workExperienceService.add(experience);

        if(serverResponse.getStatus() ==1){
            serverResponse =  workExperienceService.getLatestByResumeId(resumeId);
        }
        return serverResponse;


    }

    @PutMapping("/workexperience")
    @ResponseBody
    public ServerResponse update(Integer workExperienceId, String startWorktime, String endWorkTime, String companyName,
                                 String jobName, String companySize,String depatement, String companyType, String jobDescription,
                                 String jobType)  {


        //数据类型转换
        Date startWorkTime =  DateUtil.string2DateYM(startWorktime);
        Date endWorktime =  DateUtil.string2DateYM(endWorkTime);

        //封装成WorkExperience 对象
        WorkExperience experience =   new WorkExperience();
        experience.setWorkExperiId(workExperienceId);
        experience.setWorkStartTime(startWorkTime);
        experience.setWorkEndTime(endWorktime);
        experience.setWorkCompanyName(companyName);
        experience.setWorkPosition(jobName);
        experience.setWorkCompanyScaler(companySize);
        experience.setWorkDepartment(depatement);
        experience.setWorkCompanyProperty(companyType);
        experience.setWorkDesc(jobDescription);
        experience.setWorkType(jobType);
        experience.setStatus(0);


        ServerResponse serverResponse = workExperienceService.modify(experience);
        if(serverResponse.getStatus() ==1){
            serverResponse =  workExperienceService.getById(workExperienceId);
        }
        return  serverResponse;
    }

    @GetMapping("/workExperience/{workExperienceId}")
    @ResponseBody
    public ServerResponse  detail(@PathVariable("workExperienceId") Integer id) throws ServletException, IOException {
        ServerResponse serverResponse =  workExperienceService.getById(id);
        return serverResponse;
    }

}
